//
//  HomePageViewController.m
//  FanTest
//
//  Created by Cheng on 12/6/28.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "HomePageViewController.h"
#import <SEMasonryView/AFJSONRequestOperation.h>
#import "VerticalCell.h"
#import "MediaParser.h"
#import "AccountParser.h"
#import "AccountInfo.h"
#import "AccountColor.h"
#import "AccountTag.h"
#import "MediaLists.h"
#import "RequestAPIFunction.h"
#import "LoadingUI.h"


#define MEDIA_TAG 200000
#define IMAGELOGO_TAG 30000000
#define IMAGETAG_TAG 100000
#define BETWEEN_IMAGE_AND_TEXT_SPACEHEIGHT 10
#define BETWEEN_TEXT_AND_BOTTOM_SPACEHEIGHT 5
#define CELL_SPACEWIDTH 10
#define CELL_SPACEHEIGHT 10
#define COLUMN_WIDTH 338

#define LOADINGVIEW_TAG 10000000
#define LOADINGIMAGE_TAG 20000000
#define CHANNEL_TAG 40000000

#define COLOR_AXIS_X 105
#define TAG_AXIS_X 175
#define LOCATION_AXIS_X 260
#define FILTER_ARROW_AXIS_X 62
#define FILTER_LOCATION_ARROW_AXIS_X 192
#define LABEL_FILTER_CHANNEL_ARROW_AXIS_X 166
#define FILTER_CHANNEL_ARROW_AXIS_X 67
#define PHOTO_ARROW_AXIS_X 37

#define QUESTION_ARROW_AXIS_X 492

#define LABEL_SHOW_ALL @"SHOW ALL"
#define LABEL_BESPOKE @"BESPOKE"
#define LABEL_BLOGS @"BLOGS"
#define LABEL_ALL_SOCIAL_MEDIA @"ALL SOCIAL MEDIA"
#define LABEL_FACEBOOK @"FACEBOOK"
#define LABEL_INSTAGRAM @"INSTAGRAM"
#define LABEL_TWITTER @"TWITTER"
#define LABEL_TUMBLR @"TUMBLR"

#define BESTREAM_API @"http://54.235.166.43/BeSpokeAPI/beStream.php"
#define GETFILTERVIEW_API @"http://54.235.166.43/BeSpokeAPI/Group/GetFilterView.php"

@implementation HomePageViewController
@synthesize SEMv_masonry,reloadButton;
@synthesize Array_AccountInfo,Array_photos;
@synthesize Vw_item, Vw_Color,Vw_Tag,Vw_Location,Vw_PhotoList;
@synthesize BK_En_object, Vw_Channel, Vw_Filter, Lab_Channel;
@synthesize GroupID, Bt_Filter, Vw_QuestionView;
@synthesize delegate;

int pageCounter = 1;
int Pages = 10;
int PageIndex = 1;
static NSString *NS_Color = @"";
static NSString *NS_Tag = @"";
static NSString *NS_Location = @"";

- (void)didReceiveMemoryWarning{
    
    NSLog(@"%s", __PRETTY_FUNCTION__);
    
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if([self isViewLoaded])
    {
        
    }
}

- (void)fetchData {
    
    // set your MasonryView to be in loading state
    self.SEMv_masonry.loading = YES;
    
    reloadButton.enabled = YES;
    
    NSMutableString *NS_GoogleReaderXML = [NSMutableString stringWithString:@""];
    MediaLists *ML_List = [[MediaLists alloc] init];
    
    NSString *NS_FaceBookID_Temp = BK_En_object.NS_FaceBookID;
    NSString *NS_FaceBookToken_Temp = BK_En_object.NS_FaceBookToken;
    NSString *NS_OauthKey_Temp = BK_En_object.NS_OauthKey;
    NSString *NS_OauthSecret_Temp = BK_En_object.NS_OauthSecret;
    NSString *NS_Tumblr_Temp = BK_En_object.NS_Tumblr;
    NSString *NS_InstagramToken_Temp = BK_En_object.NS_InstagramToken;
    
    //nil convert @""
    if (((BK_En_object.NS_FaceBookID==nil)&&(BK_En_object.NS_FaceBookToken==nil))||(BK_En_object.FacebookFeedCheck==NO)) {
        NS_FaceBookID_Temp = @"";
        NS_FaceBookToken_Temp = @"";
    }
    if (((BK_En_object.NS_OauthKey==nil)&&(BK_En_object.NS_OauthSecret==nil))||(BK_En_object.TwitterFeedCheck == NO)) {
        NS_OauthKey_Temp = @"";
        NS_OauthSecret_Temp = @"";
    }
    if ((BK_En_object.TumblrFeedCheck==NO)||(BK_En_object.TumblrFeedCheck==NO))
        NS_Tumblr_Temp = @"";
    if ((BK_En_object.NS_InstagramToken==nil)||(BK_En_object.InstagramFeedCheck==NO))
        NS_InstagramToken_Temp = @"";
    
    int GoogleFeedIndex = 0;
    for (NSString *NS_href in BK_En_object.Array_GoogleReaderLink) {
        
        if ([[BK_En_object.Array_GoogleSwitchCheck objectAtIndex:GoogleFeedIndex] integerValue]==1)
            [NS_GoogleReaderXML appendString:[NSString stringWithFormat:@"<googleReader feed=\"%@\" />",NS_href]];
        
        GoogleFeedIndex++;
    }
    
    //Click Color, Tag and Location.
    if ((![NS_Color isEqualToString:@""])||(![NS_Tag isEqualToString:@""])||(![NS_Location isEqualToString:@""])) {
        NS_FaceBookID_Temp = @"";
        NS_FaceBookToken_Temp = @"";
        NS_OauthKey_Temp = @"";
        NS_OauthSecret_Temp = @"";
        NS_Tumblr_Temp = @"";
        NS_InstagramToken_Temp = @"";
        [NS_GoogleReaderXML setString:@""];
    }
    
    NSString *Stream_XML;
    
    if ((self.Lab_Channel.text!=nil)&&(![self.Lab_Channel.text isEqualToString:LABEL_SHOW_ALL]))
        Stream_XML =
        [self SetFaceBookID:NS_FaceBookID_Temp
           SetFaceBookToken:NS_FaceBookToken_Temp
         SetTwitterOauthKey:NS_OauthKey_Temp
      SetTwitterOauthSecret:NS_OauthSecret_Temp
                  SetTumblr:NS_Tumblr_Temp
          SetInstagramToken:NS_InstagramToken_Temp SetGoogleFeed:NS_GoogleReaderXML];
    else
        Stream_XML = [NSString stringWithFormat:@"<bespoke UserName=\"%@\" GroupID=\"\" /><facebook ID=\"%@\" token=\"%@\" /><twitter key=\"%@\" secret=\"%@\" /><tumblr name=\"%@\" /><instagram token=\"%@\" />%@",
                      BK_En_object.userName,
                      NS_FaceBookID_Temp,
                      NS_FaceBookToken_Temp,
                      NS_OauthKey_Temp,
                      NS_OauthSecret_Temp,
                      NS_Tumblr_Temp,
                      NS_InstagramToken_Temp,
                      NS_GoogleReaderXML];
    
    NSData *responseData = [RequestAPIFunction XML_PostRequest:[NSString stringWithFormat:@"<Stream Pages=\"%d\" PageIndex=\"%d\" Color=\"%@\" Tags=\"%@\" Location=\"%@\">%@</Stream>",
                                                                Pages,
                                                                PageIndex,
                                                                NS_Color,
                                                                NS_Tag,
                                                                NS_Location,
                                                                [Stream_XML stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"]]
                                                     urlString:BESTREAM_API];
    
    /***Test***/
    //NSData *responseData = 0;
    
    //No Network Connection or time out.
    if (responseData == nil) {
        
        self.SEMv_masonry.loading = NO;
        reloadButton.enabled = YES;
        return;
    }
    
    ML_List = [[MediaParser loadMediaLists:responseData] objectAtIndex:0];
    
    //Check Location.
    if ((ML_List.MediaArray.count==0)&&(![NS_Location isEqualToString:@""])) {
        
        [NSThread detachNewThreadSelector:@selector(threadStopAnimating) toTarget:self withObject:nil];
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Alert"
                                                           message:@"Sorry your location search hasn't produced any results. Try again!"
                                                          delegate:nil
                                                 cancelButtonTitle:@"Ok"
                                                 otherButtonTitles:nil];
        [alertView show];
        return;
    }
    else if ((![NS_Location isEqualToString:@""])&&(PageIndex==1)) {
        
        [self.Array_photos removeAllObjects];
        [self.SEMv_masonry clearCells];
    }
    
    for (MediaListsMedia *Media in ML_List.MediaArray) {
        
        //Add each of the photo object to your array.
        [self.Array_photos addObject:Media];
        
        SEMasonryCell *SEM_cell = [[[NSBundle mainBundle] loadNibNamed:@"VerticalCell" owner:self options:nil] objectAtIndex: 0];
        SEM_cell.horizontalModeEnabled = NO;
        
        SEM_cell.tag = [self.Array_photos indexOfObject:Media]+MEDIA_TAG;
        
        [SEM_cell updateCellInfo:Media];
        
        //Check Media is in MyBooks.
        NSMutableArray *Array_Book = ((AccountInfo *)[self.Array_AccountInfo objectAtIndex:0]).MyBoard.BoardArray;
        for (AccountBoard *AB_Book in Array_Book) {
            for (MediaListsMedia *Media_InBook in AB_Book.MediaArray) {
                
                if (((Media_InBook.MediaID==Media.MediaID)&&([Media_InBook.NS_Source isEqualToString:@"TPN"])&&([Media.NS_Source isEqualToString:@"TPN"]))||(([Media_InBook.NS_BeMediaID isEqualToString:Media.NS_BeMediaID])&&(![Media.NS_Source isEqualToString:@"TPN"])&&(![Media.NS_Source isEqualToString:@"TPN"])))
                    [SEM_cell viewWithTag:IMAGETAG_TAG].hidden = NO;
            }
        }
        
        UILabel *Lab_Title;
        UIImageView *IVw_Logo;
        
        //Adjust Cell.
        for (id UI in SEM_cell.subviews) {
            if ([UI isKindOfClass:[UILabel class]]) {
                Lab_Title = UI;
                Lab_Title.frame = CGRectMake(Lab_Title.frame.origin.x,
                                      SEM_cell.imageHeight+BETWEEN_IMAGE_AND_TEXT_SPACEHEIGHT,
                                      Lab_Title.frame.size.width,
                                      Lab_Title.frame.size.height);
            }
            else if (([UI isKindOfClass:[UIImageView class]])&&(((UIImageView *)UI).tag==IMAGELOGO_TAG)) {
                IVw_Logo = UI;
                IVw_Logo.frame = CGRectMake(IVw_Logo.frame.origin.x,
                                            SEM_cell.imageHeight+BETWEEN_IMAGE_AND_TEXT_SPACEHEIGHT,
                                            IVw_Logo.frame.size.width,
                                            IVw_Logo.frame.size.height);
            }
        }
        
        Lab_Title.frame = CGRectMake(Lab_Title.frame.origin.x,
                                     SEM_cell.imageHeight+BETWEEN_IMAGE_AND_TEXT_SPACEHEIGHT,
                                     Lab_Title.frame.size.width,
                                     Lab_Title.frame.size.height);
        
        int Cell_Height;
        
        if (Lab_Title.frame.size.height>IVw_Logo.frame.size.height)
            Cell_Height = SEM_cell.frame.size.height+BETWEEN_TEXT_AND_BOTTOM_SPACEHEIGHT;
        else
            Cell_Height = SEM_cell.frame.size.height+BETWEEN_IMAGE_AND_TEXT_SPACEHEIGHT+BETWEEN_TEXT_AND_BOTTOM_SPACEHEIGHT;
            
            
        [SEM_cell setFrame:CGRectMake((int)self.Vw_item.frame.size.width%COLUMN_WIDTH,
                                      CELL_SPACEHEIGHT,
                                      SEM_cell.frame.size.width,
                                      Cell_Height)];

        
        // add cell to MasonryView
        [self.SEMv_masonry addCell:SEM_cell];
    }
    
    // you are done loading, so turn off MasonryView's loading state
    self.SEMv_masonry.loading = NO;
    reloadButton.enabled = YES;
    
    if (ML_List.MediaArray.count!=0)
        PageIndex++;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if (self.BK_En_object.NonFirstUseCheck==FALSE)
        [self performSegueWithIdentifier:@"FeedSet_Segue" sender:self];

    //Clean Cell View.
    [Vw_PhotoList removeFromSuperview];
    [Vw_Channel removeFromSuperview];
    [Vw_Tag removeFromSuperview];
    [Vw_Color removeFromSuperview];
    [Vw_Location removeFromSuperview];
    [Vw_QuestionView removeFromSuperview];
    SEMv_masonry.userInteractionEnabled = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.Lab_Channel setFont:[UIFont fontWithName:@"EamesCenturyModern-Regular" size:11.5]];
    
    self.GroupID = -1;
    
    PageIndex = 1;
    
    // initialize an empty array of objects
    self.Array_photos = [[NSMutableArray alloc] init];
    
    // create a MasonryView in any size you want
    self.SEMv_masonry = [[SEMasonryView alloc] initWithFrame:CGRectMake(0, 0, self.Vw_item.bounds.size.width, self.Vw_item.bounds.size.height)];
    // set delegate to self
    self.SEMv_masonry.delegate = self;
    
    self.SEMv_masonry.columnWidth = COLUMN_WIDTH;
    //self.SEMv_masonry.rowHeight = 1000;
    
    // enable paging
    self.SEMv_masonry.loadMoreEnabled = YES;
    
    // optional
    self.SEMv_masonry.horizontalModeEnabled = NO;
    
    [Vw_item addSubview:self.SEMv_masonry];
    
    if (self.BK_En_object.NonFirstUseCheck==TRUE) {
        
        [self fetchData];
    }
    
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    
    // reorder items when there is an orientation change
    [self.SEMv_masonry layoutCells];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

#pragma mark - Interface Orientation Management

//For iOS 5
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}

//For iOS 6
-(BOOL)shouldAutorotate {
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

#pragma mark - Bottom Feature

-(IBAction)MyPhotoPressed:(id)sender {
    
    SEMv_masonry.userInteractionEnabled = NO;
    if (Vw_PhotoList == nil) {
        
        UIButton *Bt_Photo = (UIButton *)sender;
        
        Vw_PhotoList = [[[NSBundle mainBundle] loadNibNamed:@"SelectMyPhoto" owner:self options:nil] objectAtIndex:0];
        Vw_PhotoList.delegate = self;
        Vw_PhotoList.BK_En_object = self.BK_En_object;
        [Vw_PhotoList GetController:self UserInfo:Array_AccountInfo];
        Vw_PhotoList.frame = CGRectMake(Bt_Photo.frame.origin.x+Bt_Photo.frame.size.width/2-PHOTO_ARROW_AXIS_X,
                                        Bt_Photo.superview.frame.origin.y-Vw_PhotoList.frame.size.height,
                                        Vw_PhotoList.frame.size.width,
                                        Vw_PhotoList.frame.size.height);
    }
    
    [Vw_PhotoList setAlpha:0];
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_PhotoList setAlpha:1.0];[self.view addSubview:Vw_PhotoList];}
                     completion:^(BOOL finished){ }];
}

-(IBAction)QuestionPressed:(id)sender {
    
    if (Vw_QuestionView == nil) {
        
        UIButton *Bt_Question = (UIButton *)sender;
        
        Vw_QuestionView = [[[NSBundle mainBundle] loadNibNamed:@"QuestionView" owner:self options:nil] objectAtIndex:0];
        Vw_QuestionView.frame = CGRectMake(Bt_Question.center.x-QUESTION_ARROW_AXIS_X,
                                        Bt_Question.superview.frame.origin.y-Vw_QuestionView.frame.size.height,
                                        Vw_QuestionView.frame.size.width,
                                        Vw_QuestionView.frame.size.height);
        
        Vw_QuestionView.delegate = self;
        [Vw_QuestionView Initialize];
    }
    
    [Vw_QuestionView setAlpha:0];
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_QuestionView setAlpha:1.0];[self.view addSubview:Vw_QuestionView];}
                     completion:^(BOOL finished){ }];
}

#pragma mark - Filter
/**
-(IBAction)categoryPressed:(id)sender {
    
    if(Vw_Category == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"Category" owner:self options:nil];
        Vw_Category.frame = CGRectMake(20, 61, Vw_Category.frame.size.width, Vw_Category.frame.size.height);
        
        //Dynamic Category.
        for (AccountChannel *AC_Channel in ((AccountInfo *)[self.Array_AccountInfo objectAtIndex:0]).Initialization.Array_Channels) {
            
            if ([AC_Channel.NS_ChannelName isEqualToString:NS_Channel]) {
                
                Vw_Category.AC_Channel = [((AccountInfo *)[self.Array_AccountInfo objectAtIndex:0]).Initialization.Array_Channels objectAtIndex:[((AccountInfo *)[self.Array_AccountInfo objectAtIndex:0]).Initialization.Array_Channels indexOfObject:AC_Channel]];
            }
            else if (([AC_Channel.NS_ChannelName isEqualToString:BK_En_object.NS_Channel])&&(NS_Channel==nil)) {
                
                Vw_Category.AC_Channel = [((AccountInfo *)[self.Array_AccountInfo objectAtIndex:0]).Initialization.Array_Channels objectAtIndex:[((AccountInfo *)[self.Array_AccountInfo objectAtIndex:0]).Initialization.Array_Channels indexOfObject:AC_Channel]];
            }
        }
        
        Vw_Category.delegate = self;
    }
    [Vw_Category setAlpha:0];
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_Category setAlpha:1.0];[self.view addSubview:Vw_Category];}
                     completion:^(BOOL finished){ }];
    
    [Vw_Color removeFromSuperview];
    [Vw_Tag removeFromSuperview];
    [Vw_Location removeFromSuperview];
}**/

-(IBAction)colorPressed:(id)sender {
    
    if(self.Vw_Color == nil) {
        
        UIButton *Bt_Color = (UIButton *)sender;
        
        [[NSBundle mainBundle] loadNibNamed:@"Color" owner:self options:nil];
        self.Vw_Color.delegate = self;
        int Axis_Y = [Vw_Color OptionArray:((AccountInfo *)[self.Array_AccountInfo objectAtIndex:0]).Initialization.ColorArray];
        self.Vw_Color.frame = CGRectMake(Bt_Color.center.x-FILTER_ARROW_AXIS_X, Bt_Color.frame.origin.y+Bt_Color.frame.size.height, Vw_Color.frame.size.width, Axis_Y);
    }
    [Vw_Color setAlpha:0];
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_Color setAlpha:1.0];[self.view addSubview:Vw_Color];}
                     completion:^(BOOL finished){ }];
    
    [Vw_Tag removeFromSuperview];
    [Vw_Location removeFromSuperview];
}

-(IBAction)tagPressed:(id)sender {
    
    if(self.Vw_Tag == nil) {
        
        UIButton *Bt_Tag = (UIButton *)sender;
        
        [[NSBundle mainBundle] loadNibNamed:@"Tag" owner:self options:nil];
        self.Vw_Tag.delegate = self;
        int Axis_Y = [self.Vw_Tag OptionArray:((AccountInfo *)[self.Array_AccountInfo objectAtIndex:0]).Initialization.TagArray];
        
        NSLog(@"Axis_Y:%d",Axis_Y);
        self.Vw_Tag.frame = CGRectMake(Bt_Tag.center.x-FILTER_ARROW_AXIS_X, Bt_Tag.frame.origin.y+Bt_Tag.frame.size.height, self.Vw_Tag.frame.size.width, Axis_Y);
    }
    [Vw_Tag setAlpha:0];
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_Tag setAlpha:1.0];[self.view addSubview:Vw_Tag];}
                     completion:^(BOOL finished){ }];
    
    [Vw_Color removeFromSuperview];
    [Vw_Location removeFromSuperview];
}

-(IBAction)locationPressed:(id)sender {
    
    if(Vw_Location == nil) {
        
        UIButton *Bt_Location = (UIButton *)sender;
        
        [[NSBundle mainBundle] loadNibNamed:@"Location" owner:self options:nil];
        Vw_Location.frame = CGRectMake(Bt_Location.center.x-FILTER_LOCATION_ARROW_AXIS_X, Bt_Location.frame.origin.y+Bt_Location.frame.size.height, Vw_Location.frame.size.width, Vw_Location.frame.size.height);
        Vw_Location.delegate = self;
        Vw_Location.Tf_Location.delegate = self.Vw_Location;
    }
    [Vw_Location setAlpha:0];
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_Location setAlpha:1.0];[self.view addSubview:Vw_Location];}
                     completion:^(BOOL finished){ }];
    
    [Vw_Color removeFromSuperview];
    [Vw_Tag removeFromSuperview];
}

-(IBAction)RandomPressed:(id)sender
{
    if (self.Array_photos.count==0)
        return;

    //Create SEMasonryCell Temp Array.
    NSMutableArray *Array_CellTemp = [[NSMutableArray alloc] init];
    for (MediaListsMedia *MediaTemp in self.Array_photos) {
        
        int index = [self.Array_photos indexOfObject:MediaTemp];
        [Array_CellTemp addObject:[self.SEMv_masonry viewWithTag:index+MEDIA_TAG]];
    }
    
    [self.SEMv_masonry clearCells];
    
    //Random mechanism.
    int i = [Array_CellTemp count];
    while(--i){
        
        int j = arc4random() % (i+1);
        [Array_CellTemp exchangeObjectAtIndex:i withObjectAtIndex:j];
    }
    
    for (SEMasonryCell *CellTemp in Array_CellTemp)
        [self.SEMv_masonry addCell:CellTemp];
    
    self.SEMv_masonry.loading = NO;
    reloadButton.enabled = YES;
    
}

-(IBAction)RefreshPressed:(id)sender
{
    [NSThread detachNewThreadSelector:@selector(threadStartAnimating) toTarget:self withObject:nil];
    NS_Color = @"";
    NS_Tag = @"";
    NS_Location = @"";
    PageIndex = 1;
    [self.Array_photos removeAllObjects];
    [self.SEMv_masonry clearCells];
    [self fetchData];
    
    [NSThread detachNewThreadSelector:@selector(threadStopAnimating) toTarget:self withObject:nil];
}

- (IBAction)ChannelPressed:(id)sender {
    
    if(Vw_Channel == nil)
    {
        Vw_Channel = [[[NSBundle mainBundle] loadNibNamed:@"ChannelView" owner:self options:nil] objectAtIndex:0];
        Vw_Channel.frame = CGRectMake(LABEL_FILTER_CHANNEL_ARROW_AXIS_X-FILTER_CHANNEL_ARROW_AXIS_X, self.Lab_Channel.frame.origin.y+self.Lab_Channel.frame.size.height, Vw_Channel.frame.size.width, Vw_Channel.frame.size.height);
        
        [Vw_Channel initArray];
        
        Vw_Channel.delegate = self;
    }
    [Vw_Channel setAlpha:0];
    
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_Channel setAlpha:1.0];[self.view addSubview:Vw_Channel];}
                     completion:^(BOOL finished){ }];
}

- (IBAction)FilterPressed:(id)sender {
    
    NSData *responseData = [RequestAPIFunction XML_PostRequest:[NSString stringWithFormat:@"<UserInfo UserName=\"%@\"/>",self.BK_En_object.userName]
                                                     urlString:GETFILTERVIEW_API];
    
    //No Network Connection or time out.
    if (responseData == nil)
        return;
    
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:responseData options:0 error:nil];
    NSArray *ListArray_doc = [doc nodesForXPath:@"//JoinGroup" error:nil];
    NSArray *Array_Group = [[ListArray_doc objectAtIndex:0] elementsForName:@"Group"];
    
    NSMutableArray *Array_JoinGroup = [[NSMutableArray alloc] init];
    
    for (GDataXMLElement * GroupElement in Array_Group) {
        
        
        int JoinGroupID = [[[GroupElement attributeForName:@"ID"] stringValue] intValue];
        NSString *NS_JoinGroupName = [[GroupElement attributeForName:@"Name"] stringValue];
        
        AccountGroup *AG_JoinGroup = [[AccountGroup alloc] ID:JoinGroupID Name:NS_JoinGroupName];
        
        [Array_JoinGroup addObject:AG_JoinGroup];
    }
    
    if(Vw_Filter == nil) {
        
        Vw_Filter = [[[NSBundle mainBundle] loadNibNamed:@"FilterView" owner:self options:nil] objectAtIndex:0];
        Vw_Filter.frame = CGRectMake(Bt_Filter.frame.origin.x, Bt_Filter.frame.origin.y-Vw_Filter.frame.size.height, Vw_Filter.frame.size.width, Vw_Filter.frame.size.height);
        Vw_Filter.delegate = self;
    }
    
    
    [Vw_Filter initArray:((AccountShareGroup *)((AccountInfo *)[self.Array_AccountInfo  objectAtIndex:0]).ShareGroup).GroupArray SetJoinGroup:Array_JoinGroup];
    
    [Vw_Filter setAlpha:0];
    
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_Filter setAlpha:1.0];[self.view addSubview:Vw_Filter];}
                     completion:^(BOOL finished){ }];
}

#pragma mark - Segue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    if([[segue identifier] isEqualToString:@"Detail_Segue"]) {
        DetailViewController *VC_detail = segue.destinationViewController;
        VC_detail.MLM_detail = (MediaListsMedia *)sender;
        VC_detail.BK_En_object = self.BK_En_object;
        VC_detail.AI_accountDetail = [self.Array_AccountInfo objectAtIndex:0];
        VC_detail.delegate = self;
    }
    else if([[segue identifier] isEqualToString:@"Mybook_Segue"]) {
        MyBoardUIViewController *VC_myboard = segue.destinationViewController;
        VC_myboard.Array_AccountInfo = self.Array_AccountInfo;
        VC_myboard.BK_En_object = self.BK_En_object;
        VC_myboard.delegate = self;
    }
    else if([[segue identifier] isEqualToString:@"Account_Segue"]) {
        AccountViewController *VC_AccountSetting = segue.destinationViewController;
        VC_AccountSetting.Array_AccountInfo = Array_AccountInfo;
        VC_AccountSetting.BK_En_object = self.BK_En_object;
        VC_AccountSetting.delegate = self;
    }
    else if([[segue identifier] isEqualToString:@"FeedSet_Segue"]) {
        FeedSetViewController *VC_FeedSet = segue.destinationViewController;
        VC_FeedSet.Array_AccountInfo = self.Array_AccountInfo;
        VC_FeedSet.BK_En_object = self.BK_En_object;
        VC_FeedSet.delegate = self;
    }
}

#pragma mark Detail_Segue
- (void) didSelectItemAtIndex:(int)index {
    
    [self performSegueWithIdentifier:@"Detail_Segue" sender:[self.Array_photos objectAtIndex:index-MEDIA_TAG]];
}

#pragma mark - SEMasonryView Delegate

- (void) didEnterLoadingMode {
    
    // fetch data again if it is dragged and released in the bottom
    
    //Each 10 page auto clear older fifty media.
    if ((self.Array_photos.count%100==0)&&(self.Array_photos.count!=0)) {
        
        NSRange Range;
        Range.location = 0;
        Range.length = 50;
        
        [self.Array_photos removeObjectsInRange:Range];
        
        //Create SEMasonryCell Temp Array and remove older media.
        NSMutableArray *Array_CellTemp = [[NSMutableArray alloc] init];
        for (MediaListsMedia *MediaTemp in self.Array_photos) {
            
            int index = [self.Array_photos indexOfObject:MediaTemp];
            
            [[self.SEMv_masonry viewWithTag:index+MEDIA_TAG] removeFromSuperview];
            [self.SEMv_masonry viewWithTag:Range.length+index+MEDIA_TAG].tag = index+MEDIA_TAG;
            [Array_CellTemp addObject:[self.SEMv_masonry viewWithTag:index+MEDIA_TAG]];
        }
        
        [self.SEMv_masonry clearCells];
        
        for (SEMasonryCell *CellTemp in Array_CellTemp)
            [self.SEMv_masonry addCell:CellTemp];
    }
    
    [NSThread detachNewThreadSelector:@selector(threadStartAnimating) toTarget:self withObject:nil];
    
    [self fetchData];
    [NSThread detachNewThreadSelector:@selector(threadStopAnimating) toTarget:self withObject:nil];
}

#pragma mark ColorDelegate

-(void) uiViewTouched_Color:(BOOL)wasInSide
{
    if(!wasInSide)
        [Vw_Color removeFromSuperview];
}

-(void) DidSelected_Color:(NSString *)Txt_Color
{
    NS_Tag = @"";
    NS_Location = @"";
    [NSThread detachNewThreadSelector:@selector(threadStartAnimating) toTarget:self withObject:nil];
    NS_Color = Txt_Color;
    PageIndex = 1;
    [self.Array_photos removeAllObjects];
    [self.SEMv_masonry clearCells];
    [self fetchData];
    [NSThread detachNewThreadSelector:@selector(threadStopAnimating) toTarget:self withObject:nil];
}

#pragma mark TagDelegate

-(void) uiViewTouched_Tag:(BOOL)wasInSide
{
    if(!wasInSide)
        [Vw_Tag removeFromSuperview];
}

-(void)DidSelected_Tag:(NSString *)Txt_Tag
{
    NS_Color = @"";
    NS_Location = @"";
    [NSThread detachNewThreadSelector:@selector(threadStartAnimating) toTarget:self withObject:nil];
    NS_Tag = Txt_Tag;
    PageIndex = 1;
    [self.Array_photos removeAllObjects];
    [self.SEMv_masonry clearCells];
    [self fetchData];
    [NSThread detachNewThreadSelector:@selector(threadStopAnimating) toTarget:self withObject:nil];
}

#pragma mark LocationDelegate

-(void) uiViewTouched_Location:(BOOL)wasInSide
{
    if(!wasInSide)
        [Vw_Location removeFromSuperview];
}

-(void)DidSelected_Location:(NSString *)Location
{
    [Vw_Location removeFromSuperview];
    [[NSOperationQueue mainQueue] addOperationWithBlock:
     ^(void) {
    NS_Color = @"";
    NS_Location = @"";
    [NSThread detachNewThreadSelector:@selector(threadStartAnimating) toTarget:self withObject:nil];
    NS_Location = Location;
    PageIndex = 1;
    [self fetchData];
    [NSThread detachNewThreadSelector:@selector(threadStopAnimating) toTarget:self withObject:nil];
     }];
}

#pragma mark ChannelViewDelegate


-(void) uiViewTouched_Channel:(BOOL)wasInSide {
    if(!wasInSide)
        [Vw_Channel removeFromSuperview];
}

-(void) ChannelDidSelected:(NSString *)Channel {
    
    Lab_Channel.text = Channel;
    
    UIAlertView *alertView;
    
    
    if ((BK_En_object.FacebookFeedCheck==NO)&&([Channel isEqualToString:LABEL_FACEBOOK])) {
        alertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Please push your Facebook switch in Setting." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alertView show];
        alertView.tag = CHANNEL_TAG;
        alertView.delegate = self;
        return;
    }
    else if ((BK_En_object.InstagramFeedCheck==NO)&&([Channel isEqualToString:LABEL_INSTAGRAM])) {
        alertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Please push your Instagram switch in Setting." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alertView show];
        alertView.tag = CHANNEL_TAG;
        alertView.delegate = self;
        return;
    }
    else if ((BK_En_object.TwitterFeedCheck==NO)&&([Channel isEqualToString:LABEL_TWITTER])) {
        alertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Please push your Twitter switch in Setting." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alertView show];
        alertView.tag = CHANNEL_TAG;
        alertView.delegate = self;
        return;
    }
    else if ((BK_En_object.TumblrFeedCheck==NO)&&([Channel isEqualToString:LABEL_TUMBLR])) {
        alertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Please push your Tumblr switch in Setting." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alertView show];
        alertView.tag = CHANNEL_TAG;
        alertView.delegate = self;
        return;
    }
    else if ([Channel isEqualToString:LABEL_BLOGS]) {
        
        //Check Feed is added.
        if (self.BK_En_object.Array_GoogleTitleSelected.count==0) {
            
            alertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Please add Google Feed in Setting." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [alertView show];
            alertView.tag = CHANNEL_TAG;
            alertView.delegate = self;
            return;
        }
        
        int Index = 0;
        for (id Check_Object in self.BK_En_object.Array_GoogleSwitchCheck) {
            Index++;
            if ([[Check_Object stringValue] isEqualToString:@"1"])
                break;
            else if (self.BK_En_object.Array_GoogleSwitchCheck.count==Index) {
                
                alertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Please push your Google Feed switch in Setting." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                [alertView show];
                alertView.tag = CHANNEL_TAG;
                alertView.delegate = self;
                return;
            }
        }
    }
    else if (([Channel isEqualToString:LABEL_ALL_SOCIAL_MEDIA])
        &&(BK_En_object.FacebookFeedCheck==NO)&&(BK_En_object.InstagramFeedCheck==NO)
        &&(BK_En_object.TwitterFeedCheck==NO)&&(BK_En_object.TumblrFeedCheck==NO)) {
        
        //Check Feed is added.
        if (self.BK_En_object.Array_GoogleTitleSelected.count==0) {
            
            alertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Please push your social feed switch in Setting." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [alertView show];
            alertView.tag = CHANNEL_TAG;
            alertView.delegate = self;
            return;
        }
        
        int Index = 0;
        for (id Check_Object in self.BK_En_object.Array_GoogleSwitchCheck) {
            Index++;
            if ([[Check_Object stringValue] isEqualToString:@"1"])
                break;
            else if (self.BK_En_object.Array_GoogleSwitchCheck.count==Index) {
                
                alertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Please push your social feed switch in Setting." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                [alertView show];
                alertView.tag = CHANNEL_TAG;
                alertView.delegate = self;
                return;
            }
        }
    }
    
    [[NSOperationQueue mainQueue] addOperationWithBlock:
     ^(void) {
         
         NS_Color = @"";
         NS_Tag = @"";
         NS_Location = @"";
         
         [NSThread detachNewThreadSelector:@selector(threadStartAnimating) toTarget:self withObject:nil];
         PageIndex = 1;
         [self.Array_photos removeAllObjects];
         [self.SEMv_masonry clearCells];
         [self fetchData];
         [NSThread detachNewThreadSelector:@selector(threadStopAnimating) toTarget:self withObject:nil];

     }];
}

#pragma mark FilterViewDelegate

-(void) uiViewTouched_Filter:(BOOL)wasInSide {
    if(!wasInSide)
        [Vw_Filter removeFromSuperview];
}

-(void) DidSelected_Fiter:(int)FilterID SetFilterName:(NSString *)NS_Filter {
    
    self.GroupID = FilterID;
    
    if (self.GroupID==-1)
        [self.Bt_Filter setTitle:@"  Filter content for:" forState:UIControlStateNormal];
    else {
        
        [self.Bt_Filter setTitle:[NSString stringWithFormat:@"  %@",NS_Filter] forState:UIControlStateNormal];
        //self.NS_Channel = @"All Bespoke";
        [self.Vw_Channel.TVw_Channel selectRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] animated:NO scrollPosition:0];
    }
    
    NS_Color = @"";
    NS_Tag = @"";
    NS_Location = @"";
    
    [NSThread detachNewThreadSelector:@selector(threadStartAnimating) toTarget:self withObject:nil];
    PageIndex = 1;
    [self.Array_photos removeAllObjects];
    [self.SEMv_masonry clearCells];
    [self fetchData];
    [NSThread detachNewThreadSelector:@selector(threadStopAnimating) toTarget:self withObject:nil];
}

#pragma mark -
#pragma mark SelectMyPhotoDelegate

-(void) uiViewTouched_SelectMyPhoto:(BOOL)wasInSide {
    if(!wasInSide) {
        [Vw_PhotoList removeFromSuperview];
        SEMv_masonry.userInteractionEnabled = YES;
    }
}

- (void)PhotoSaveSuccess:(NSString *)BoardName {
    [Vw_PhotoList removeFromSuperview];
    SEMv_masonry.userInteractionEnabled = YES;
        
    NSString *NS_Message = [NSString stringWithFormat:@"Great! This photo has been clipped to your \"%@\" Books",BoardName];
    
    
    UIAlertView *AlertView = [[UIAlertView alloc]initWithTitle:@"Success" message:NS_Message delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];

    [AlertView show];
}

#pragma mark -
#pragma mark DetailViewcontrollerDelegate

-(void)DetailViewControllerDidClose:(DetailViewController *)controller
{
    [self dismissViewControllerAnimated:YES completion:nil];
    [NSThread detachNewThreadSelector:@selector(threadStopAnimating) toTarget:self withObject:nil];
    
    //Check Media is in MyBooks.
    NSMutableArray *Array_Book = ((AccountInfo *)[self.Array_AccountInfo objectAtIndex:0]).MyBoard.BoardArray;
    for (AccountBoard *AB_Book in Array_Book) {
        for (MediaListsMedia *Media_InBook in AB_Book.MediaArray) {
            if (((Media_InBook.MediaID==controller.MLM_detail.MediaID)&&([Media_InBook.NS_Source isEqualToString:@"TPN"])&&([controller.MLM_detail.NS_Source isEqualToString:@"TPN"]))||
                (([Media_InBook.NS_BeMediaID isEqualToString:controller.MLM_detail.NS_BeMediaID])&&(![controller.MLM_detail.NS_Source isEqualToString:@"TPN"]))) {
                [[self.SEMv_masonry viewWithTag:[self.Array_photos indexOfObject:controller.MLM_detail]+MEDIA_TAG] viewWithTag:IMAGETAG_TAG].hidden = NO;
            }
        }
    }
}

#pragma mark MyBoardViewControllerDelegate

-(void)MyBoardUIViewControllerDidStream:(UIViewController *)controller {
    
    [self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
    
    //Hide All Tag Icon.
    for (MediaListsMedia *Media in self.Array_photos)
        [[self.SEMv_masonry viewWithTag:[self.Array_photos indexOfObject:Media]+MEDIA_TAG] viewWithTag:IMAGETAG_TAG].hidden = YES;
    
    //Check Media is in MyBooks.
    NSMutableArray *Array_Book = ((AccountInfo *)[self.Array_AccountInfo objectAtIndex:0]).MyBoard.BoardArray;
    for (AccountBoard *AB_Book in Array_Book)
        for (MediaListsMedia *Media_InBook in AB_Book.MediaArray)
            for (MediaListsMedia *Media_inPhotos in self.Array_photos)
                if (((Media_InBook.MediaID==Media_inPhotos.MediaID)&&([Media_InBook.NS_Source isEqualToString:@"TPN"])&&([Media_inPhotos.NS_Source isEqualToString:@"TPN"]))||
                        (([Media_InBook.NS_BeMediaID isEqualToString:Media_inPhotos.NS_BeMediaID])&&(![Media_inPhotos.NS_Source isEqualToString:@"TPN"]))) {
                    [[self.SEMv_masonry viewWithTag:[self.Array_photos indexOfObject:Media_inPhotos]+MEDIA_TAG] viewWithTag:IMAGETAG_TAG].hidden = NO;
                }
}
-(void)MyBoardUIViewControllerDidAccount:(UIViewController *)controller {
    
    [controller dismissViewControllerAnimated:NO completion:nil];
    [self performSegueWithIdentifier:@"Account_Segue" sender:self];
}

-(void)MyBoardUIViewControllerDidFeedSet:(UIViewController *)controller {
    
    [self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
    [self performSegueWithIdentifier:@"FeedSet_Segue" sender:self];
}

#pragma mark AccountViewControllerDelegate

-(void)AccountViewControllerDelegateDidFeedSet:(AccountViewController *)controller {
    
    Vw_Filter = nil;
    [self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
    [self performSegueWithIdentifier:@"FeedSet_Segue" sender:self];
}

-(void)AccountViewControllerDelegateDidMyBooks:(AccountViewController *)controller {
    
    Vw_Filter = nil;
    [controller dismissViewControllerAnimated:NO completion:nil];
    [self performSegueWithIdentifier:@"Mybook_Segue" sender:self];
}

-(void)AccountViewControllerDelegateDidLogoScreen:(AccountViewController *)controller {
    
    [self.delegate HomePageViewControllerDelegateDidLogoScreen:self];
}

-(void)AccountViewControllerDelegateDidStream:(AccountViewController *)controller {
    
    Vw_Filter = nil;
    //controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    
    NS_Color = @"";
    NS_Tag = @"";
    NS_Location = @"";
    PageIndex = 1;
    
    [self.Array_photos removeAllObjects];
    [self.SEMv_masonry clearCells];
    [self fetchData];
    [controller dismissViewControllerAnimated:NO completion:nil];
}

#pragma mark FeedSetViewControllerDelegate

-(void)FeedSetViewControllerDelegateDidAccount:(FeedSetViewController *)controller {
    
    [self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
    [self performSegueWithIdentifier:@"Account_Segue" sender:self];
}

-(void)FeedSetViewControllerDelegateDidMyBooks:(FeedSetViewController *)controller {
    
    [controller dismissViewControllerAnimated:NO completion:nil];
    [self performSegueWithIdentifier:@"Mybook_Segue" sender:self];
}


-(void)FeedSetViewControllerDelegateDidStream:(FeedSetViewController *)controller {
    
    [controller dismissViewControllerAnimated:NO completion:nil];
}

-(void)FeedSetViewControllerDelegateDidLoadingStream:(FeedSetViewController *)controller {
    
    //controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    
    NS_Color = @"";
    NS_Tag = @"";
    NS_Location = @"";
    PageIndex = 1;
    
    [self.Vw_Channel.TVw_Channel reloadData];
    self.Lab_Channel.text = LABEL_SHOW_ALL;
    
    [self.Array_photos removeAllObjects];
    [self.SEMv_masonry clearCells];
    [self fetchData];
    
    [NSThread detachNewThreadSelector:@selector(threadStopAnimating) toTarget:self withObject:nil];
    [controller dismissViewControllerAnimated:NO completion:nil];
}

-(void)FeedSetViewControllerDelegateDidLogoScreen:(FeedSetViewController *)controller {
    
    [self.delegate HomePageViewControllerDelegateDidLogoScreen:self];
}

#pragma mark - QuestionViewDelegate

- (void)uiViewTouched_Question:(BOOL)wasInSide {
    
    if(!wasInSide)
        [Vw_QuestionView removeFromSuperview];
}

#pragma mark - Channel Function

- (NSString *)SetFaceBookID:(NSString *)FaceBookID SetFaceBookToken:(NSString *)FaceBookToken SetTwitterOauthKey:(NSString *)TwitterOauthKey SetTwitterOauthSecret:(NSString *)TwitterOauthSecret SetTumblr:(NSString *)Tumblr SetInstagramToken:(NSString *)InstagramToken SetGoogleFeed:(NSString *)GoogleFeed {
    
    NSString *NS_XML;
    
    if ([self.Lab_Channel.text isEqualToString:LABEL_BESPOKE])
        NS_XML = [NSString stringWithFormat:@"<bespoke UserName=\"%@\" />",BK_En_object.userName];
    else if ([self.Lab_Channel.text isEqualToString:LABEL_BLOGS])
        NS_XML = GoogleFeed;
    else if ([self.Lab_Channel.text isEqualToString:LABEL_FACEBOOK])
        NS_XML = [[NSString stringWithFormat:@"<facebook ID=\"%@\" token=\"%@\" />",FaceBookID, FaceBookToken] stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"];
    else if ([self.Lab_Channel.text isEqualToString:LABEL_INSTAGRAM])
        NS_XML = [[NSString stringWithFormat:@"<instagram token=\"%@\" />",InstagramToken] stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"];
    else if ([self.Lab_Channel.text isEqualToString:LABEL_TWITTER])
        NS_XML = [[NSString stringWithFormat:@"<twitter key=\"%@\" secret=\"%@\" />",TwitterOauthKey, TwitterOauthSecret] stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"];
    else if ([self.Lab_Channel.text isEqualToString:LABEL_TUMBLR])
        NS_XML = [NSString stringWithFormat:@"<tumblr name=\"%@\" />",Tumblr];
    else if ([self.Lab_Channel.text isEqualToString:LABEL_ALL_SOCIAL_MEDIA])
        NS_XML = [[NSString stringWithFormat:@"<facebook ID=\"%@\" token=\"%@\" /><instagram token=\"%@\" /><twitter key=\"%@\" secret=\"%@\" /><tumblr name=\"%@\" />%@",
                   FaceBookID,
                   FaceBookToken,
                   InstagramToken,
                   TwitterOauthKey,
                   TwitterOauthSecret,
                   Tumblr,
                   GoogleFeed
                   ] stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"];
    else
        NS_XML = [NSString stringWithFormat:@"<bespoke UserName=\"%@\" Channel=\"%@\" GroupID=\"%d\" />",
                      BK_En_object.userName,
                      self.Lab_Channel.text,
                      GroupID];
    
    return NS_XML;
}

#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    
    if (alertView.tag==CHANNEL_TAG) {
        self.Lab_Channel.text = LABEL_SHOW_ALL;
        [self.Vw_Channel.TVw_Channel reloadData];
        [self performSegueWithIdentifier:@"FeedSet_Segue" sender:self];
    }
}

#pragma mark - LoadingUI

-(void)threadStartAnimating {
    
    [self.view addSubview:[LoadingUI LoadingView:self.view.bounds.size.width SetHeight:self.view.bounds.size.height]];
    [self.view addSubview:[LoadingUI LoadingImage:self.view.bounds.size.width SetHeight:self.view.bounds.size.height]];
}

-(void)threadStopAnimating {
    
    [[NSOperationQueue mainQueue] addOperationWithBlock:
     ^(void) {
         [[self.view viewWithTag:LOADINGVIEW_TAG] removeFromSuperview];
         [[self.view viewWithTag:LOADINGIMAGE_TAG] removeFromSuperview];
     }];
}

@end
